package Thread;

/**
 * Created with IntelliJ IDEA.
 * Description: synchronized使用方式
 */
class Counter {
    private int count = 0;
    private Object object = new Object();

    public int getCount() {
        return count;
    }

    public synchronized void countAdd() {
        synchronized (object) {
            count++;
        }
    }
}

public class ThreadDemo17 {
    static Counter counter = new Counter();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                for(int i = 0; i < 50000; i++) {
                    counter.countAdd();
                }
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                for(int i = 0; i < 50000; i++) {
                    counter.countAdd();
                }
            }
        };
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter.getCount());
    }
}
